package com.jule.miaozhuapc.dao.impl;

import java.util.List;

import com.jule.miaozhuapc.domain.WawaRecharge;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.jule.miaozhuapc.dao.RechargeDao;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Repository
public class RechargeDAOImpl  implements RechargeDao{

	private EntityManager em;
	@PersistenceContext
	public void setEm(EntityManager em) {
		this.em = em;
	}
	@Override
	public WawaRecharge findMaxRecharge() {

		String hql = "from WawaRecharge where isDelete = 0 order by addTime desc, orderNo desc";
		WawaRecharge wawaRecharge = em.createQuery(hql, WawaRecharge.class)
				.setFirstResult(0)
				.setMaxResults(1)
				.getSingleResult();
		return wawaRecharge;
	}

	@Override
	public WawaRecharge insert(WawaRecharge wawaRecharge) {

		em.persist(wawaRecharge);
		return wawaRecharge;
	}

	@Override
	public WawaRecharge findbyOrderNo(String orderNo) {
		String hql = "from WawaRecharge where orderNo =:orderNo";
		List<WawaRecharge> wawaRecharges = em.createQuery(hql, WawaRecharge.class)
				.setParameter("orderNo", orderNo)
				.getResultList();

		if(wawaRecharges != null && wawaRecharges.size() > 0){
			return  wawaRecharges.get(0);
		}
		return null;
	}

	@Override
	public WawaRecharge update(WawaRecharge wawaRecharge) {
		em.merge(wawaRecharge);
		return  wawaRecharge;
	}
}
